package com.fire.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fire.bo.coupon.req.CouponReq;
import com.fire.bo.coupon.req.CouponReqAdd;
import com.fire.bo.coupon.req.DelCouponReq;
import com.fire.bo.coupon.req.SelCouponReq;
import com.fire.bo.coupon.res.CouponRes;
import com.fire.common.RspObjBean;
import com.fire.service.CouponService;
import com.fire.util.BussinessUtils;
import com.fire.util.ErrRsp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@CrossOrigin
@RequestMapping("/pc/coupon")
@Api(value = "优惠卷管理", description = "优惠卷管理")
public class CouponController
{
	@Autowired
	private CouponService couponService;
	
	/**
	 * 新增优惠卷
	 * 
	 * @param reqPersonEntryDateBean
	 * @param bindingResult
	 * @return
	 * @version:v1.0
	 * @author:ZhangPeng
	 * @date:2017年06月02日 上午09:07:14
	 */
	@ApiOperation(value = "新增优惠卷", notes = "新增优惠卷")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public @ResponseBody RspObjBean add(
			@RequestBody @Valid CouponReqAdd bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}

		try
		{
			couponService.addCoupon(bean);
			// 根据上级类别key得到类别集合
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
					ErrRsp.SUCCESS_CODE_DESC, null);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
	
	/**
	 * 修改优惠卷
	 * 
	 * @param reqPersonEntryDateBean
	 * @param bindingResult
	 * @return
	 * @version:v1.0
	 * @author:ZhangPeng
	 * @date:2017年06月02日 上午09:07:14
	 */
	@ApiOperation(value = "修改优惠卷", notes = "优惠")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody RspObjBean update(
			@RequestBody @Valid CouponRes bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}

		try
		{
			couponService.updateCoupon(bean);
			// 根据上级类别key得到类别集合
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
					ErrRsp.SUCCESS_CODE_DESC, null);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
	
	/**
	 * 删除优惠卷
	 * 
	 * @param reqPersonEntryDateBean
	 * @param bindingResult
	 * @return
	 * @version:v1.0
	 * @author:ZhangPeng
	 * @date:2017年06月02日 上午09:07:14
	 */
	@ApiOperation(value = "删除优惠卷", notes = "优惠")
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public @ResponseBody RspObjBean del(
			@RequestBody @Valid DelCouponReq bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}

		try
		{
			couponService.delCoupon(bean);
			// 根据上级类别key得到类别集合
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
					ErrRsp.SUCCESS_CODE_DESC, null);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
	
	/**
	 * 优惠卷列表
	 * 
	 * @param reqPersonEntryDateBean
	 * @param bindingResult
	 * @return
	 * @version:v1.0
	 * @author:ZhangPeng
	 * @date:2017年06月02日 上午09:07:14
	 */
	@ApiOperation(value = "优惠卷列表", notes = "优惠",response = CouponReq.class)
	@RequestMapping(value = "/selCoupon", method = RequestMethod.POST)
	public @ResponseBody RspObjBean selCoupon(
			@RequestBody @Valid SelCouponReq bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}

		try
		{
			List<CouponReq> list = couponService.selCoupon(bean);
			// 根据上级类别key得到类别集合
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
					ErrRsp.SUCCESS_CODE_DESC, list,couponService.selCouponCount(bean));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
	
	/**
	 * 优惠卷单个
	 * 
	 * @param reqPersonEntryDateBean
	 * @param bindingResult
	 * @return
	 * @version:v1.0
	 * @author:ZhangPeng
	 * @date:2017年06月02日 上午09:07:14
	 */
	@ApiOperation(value = "优惠卷单个", notes = "优惠",response = CouponRes.class)
	@RequestMapping(value = "/selCouponById", method = RequestMethod.POST)
	public @ResponseBody RspObjBean selCouponById(
			@RequestBody @Valid DelCouponReq bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}

		try
		{
			CouponRes info = couponService.selCouponById(bean);
			// 根据上级类别key得到类别集合
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
					ErrRsp.SUCCESS_CODE_DESC, info);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
}
